﻿using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
using DG.Tweening;

namespace BToolkit.ExeUtils
{
    public class BgCarousel : MonoBehaviour
    {
        public class Content
        {
            public Texture texture;
            public string videoPath;
        }
        public RawImage img1, img2;
        public string folder;

        public RectTransform rectTransform { get; set; }
        VideoPlayer video;
        List<Content> contents = new List<Content>();
        RawImage currImg, anotherImg;
        bool haveMultiContent;
        int currShowIndex;
        bool prevousIsVideo;
        float changeInterval, changeDuration, changeTimer;
        string BaseName { get { return transform.parent ? transform.parent.name + "_" + GetType().Name : GetType().Name; } }
        float ChangeInterval
        {
            get { return DataStorage.GetFloat(BaseName + "_ChangeInterval", 10); }
            set { DataStorage.SetFloat(BaseName + "_ChangeInterval", value); }
        }
        float ChangeDuration
        {
            get { return DataStorage.GetFloat(BaseName + "_ChangeDuration", 2); }
            set { DataStorage.SetFloat(BaseName + "_ChangeDuration", value); }
        }

        void Awake()
        {
            rectTransform = transform as RectTransform;
            img1.color = new Color(1, 1, 1, 0);
            img2.color = new Color(1, 1, 1, 0);
            video = GetComponent<VideoPlayer>();
            if (!video) video = gameObject.AddComponent<VideoPlayer>();
            if (!video.targetTexture) video.targetTexture = new RenderTexture(1280, 720, 32);
            video.isLooping = true;
            currImg = img1;
            anotherImg = img2;
        }

        private void OnEnable()
        {
            ReadStorageData();
        }

        void ReadStorageData()
        {
            changeInterval = ChangeInterval;
            changeDuration = ChangeDuration;
        }

        void Start()
        {
            FileInfo[] fileInfos = MainController.GetFiles(MainController.resPath + "/" + folder);
            for (int i = 0; i < fileInfos.Length; i++)
            {
                string filePath = fileInfos[i].FullName.Replace("\\", "/");
                Content content = new Content();
                if (filePath.EndsWith(".mp4") || filePath.EndsWith(".webm"))
                {
                    content.videoPath = filePath;
                }
                else
                {
                    content.texture = MainController.GetTexture(filePath);
                }
                contents.Add(content);
            }
            haveMultiContent = contents.Count > 1;
            ShowNext();
        }

        void Update()
        {
            if (haveMultiContent)
            {
                changeTimer += Time.deltaTime;
                if (changeTimer >= changeInterval)
                {
                    changeTimer = 0f;
                    ShowNext();
                }
            }
        }

        void ShowNext()
        {
            currImg = currImg == img1 ? img2 : img1;
            anotherImg = currImg == img1 ? img2 : img1;
            ShowContent(contents[currShowIndex]);
            currShowIndex++;
            if (currShowIndex > contents.Count - 1)
            {
                currShowIndex = 0;
            }
        }

        public void ShowContent(Content content)
        {
            bool currIsVideo = false;
            if (content.texture)
            {
                currImg.texture = content.texture;
            }
            else
            {
                currIsVideo = true;
                video.url = "file://" + content.videoPath;
                currImg.texture = video.targetTexture;
            }
            if (prevousIsVideo && currIsVideo)
            {
                //两个都是视频
                currImg.enabled = true;
                anotherImg.enabled = false;
                currImg.color = Color.white;
            }
            else if (!prevousIsVideo && currIsVideo)
            {
                //上一个图片当前视频
                currImg.enabled = true;
                anotherImg.enabled = true;
                anotherImg.rectTransform.SetAsLastSibling();
                anotherImg.color = Color.white;
                anotherImg.DOColor(new Color(1, 1, 1, 0), changeDuration).SetEase(Ease.Linear).OnComplete(() =>
                {
                    anotherImg.enabled = false;
                });
            }
            else if ((prevousIsVideo && !currIsVideo) || (!prevousIsVideo && !currIsVideo))
            {
                //上一个视频当前图片 或 两个都是图片
                currImg.enabled = true;
                anotherImg.enabled = true;
                currImg.rectTransform.SetAsLastSibling();
                currImg.color = new Color(1, 1, 1, 0);
                currImg.DOColor(Color.white, changeDuration).SetEase(Ease.Linear).OnComplete(() =>
                {
                    anotherImg.enabled = false;
                    video.Stop();
                });
            }
            prevousIsVideo = !content.texture;
        }
    }
}